<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" ref="searchForm" :model="searchForm">
            <el-form-item prop="alarmNo" label="报警编号:">
              <el-input v-model="searchForm.alarmNo" placeholder="请输入地址"></el-input>
            </el-form-item>
            <el-form-item prop="alarmType" label="报警类型:">
              <el-select v-model="searchForm.alarmType" :clearable="true" placeholder="请选择类型">
                <el-option v-for="item in typeList_s" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="alarmState" label="报警状态:">
              <el-select v-model="searchForm.alarmState" :clearable="true" placeholder="请选择状态">
                <el-option v-for="item in statusList_s" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="streetIds" label="地区:">
              <Region :streetValues="searchForm.streetIds" @regionChange="handleItemChange" :isChangeOnSelect="true"> </Region>
            </el-form-item>

            <el-form-item prop="villageId" label="小区:">
              <el-select filterable v-model="searchForm.villageId" :clearable="true" placeholder="请选择小区">
                <el-option v-for="item in villageList_s" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
            <br />

            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>
    <el-row ref="tableRow">
      <el-table ref="tableList" :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column label="报警时间" align="center" width="140">
          <template slot-scope="scope">
            {{ scope.row.createDate }}
          </template>
        </el-table-column>
        <el-table-column prop="alarmNo" align="center" label="报警编号" width="auto"> </el-table-column>
        <el-table-column prop="beeperCode" align="center" label="呼叫器编号" width="auto"> </el-table-column>
        <el-table-column align="center" label="联系人/电话" width="auto">
          <template slot-scope="scope"> {{ scope.row.linkName }} - {{ scope.row.phone }} </template>
        </el-table-column>
        <el-table-column align="center" label="物业/电话" width="auto">
          <template slot-scope="scope"> {{ scope.row.pmcName }} - {{ scope.row.pmcPhone }} </template>
        </el-table-column>
        <el-table-column align="center" label="报警地址" width="240">
          <template slot-scope="scope">
            {{ scope.row.provinceName }}{{ scope.row.cityName }}{{ scope.row.areaName }}{{ scope.row.streetName }}
            {{ scope.row.villageName }}
            {{ scope.row.buildName }}{{ scope.row.unitName }}{{ scope.row.floorName }}{{ scope.row.houseNumber }}
          </template>
        </el-table-column>
        <el-table-column prop="exitAddress" align="center" label="通道入口地址" width="200"> </el-table-column>
        <el-table-column align="center" label="是否拨打电话" width="200">
          <template slot-scope="scope">
            {{ scope.row.callState === 1 ? '已拨打' : '未拨打' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="报警类型" width="100">
          <template slot-scope="scope">
            {{ scope.row.alarmType + '报警' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="报警状态" width="100">
          <template slot-scope="scope">
            {{ scope.row.alarmState | statusFilters }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="报警来源" width="100">
          <template slot-scope="scope">
            {{ scope.row.alarmSource | alarmSourceFilters }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-row>
              <el-button size="mini" v-has="permissionView" @click="operateClick(scope.row, 1)">查看</el-button>
              <el-button size="mini" v-show="scope.row.alarmState !== 15 && scope.row.alarmState !== 20" v-has="permissionCancel" @click="operateClick(scope.row, 2)">取消</el-button>
              <el-button size="mini" v-show="scope.row.alarmState !== 15 && scope.row.alarmState !== 20 && scope.row.alarmState !== 16" v-has="permissionConfirm" @click="operateClick(scope.row, 3)">完成</el-button>
              <div style="margin-top: 10px">
                <el-button size="mini" v-has="permissionCall" v-show="scope.row.callState === 0" @click="operateClick(scope.row, 4)">已拨打</el-button>
              </div>
              <!-- 报告单操作 -->
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import alarmApi from '@/api/message/alarm';
import VillageApi from '@/api/address/village';
import Region from '@/components/Region';
export default {
  name: 'AlarmListManagement',
  inject: ['reload'], //注入局部刷新
  components: {
    Region,
  },
  data() {
    return {
      page: {
        pageNo: 1,
        pageSize: 20,
        total: 0,
      },
      searchForm: {
        alarmNo: '',
        alarmType: '',
        alarmState: '',
        streetIds: [],
        villageId: '',
        provinceId: '',
        cityId: '',
        areaId: '',
        streetId: '',
      },
      typeList_s: [
        { value: '119', label: '119' },
        { value: '120', label: '120' },
        { value: '110', label: '110' },
      ],
      statusList_s: [
        { value: '1', label: '待出警' },
        { value: '5', label: '已出警' },
        { value: '15', label: '已取消' },
        { value: '16', label: '取消失败' },
        { value: '20', label: '已完成' },
        // { value: "20", label: "已拨打" },
      ],
      villageList_s: [],

      tableList: [],
      permissionRepeat: 'SYSTEM_MANAGER_MESSAGE_ALARM_LIST_REPEAT',
      permissionView: 'SYSTEM_MANAGER_MESSAGE_ALARM_LIST_VIEW',
      permissionCancel: 'SYSTEM_MANAGER_MESSAGE_ALARM_LIST_CANCEL',
      permissionConfirm: 'SYSTEM_MANAGER_MESSAGE_ALARM_LIST_CONFIRM',
      permissionCall: 'SYSTEM_MANAGER_MESSAGE_ALARM_LIST_CALL',
    };
  },
  mounted() {
    window.addEventListener('keydown', this.keyDown);
    this.listTable();
    this.getVillageList();
    this.getTableHeight();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    handleItemChange(val) {
      this.searchForm.provinceId = val.provinceId;
      this.searchForm.cityId = val.cityId;
      this.searchForm.areaId = val.areaId;
      this.searchForm.streetId = val.streetId;
      if (val.provinceId) {
        this.searchForm.streetIds = [val.provinceId, val.cityId, val.areaId, val.streetId];
      } else {
        this.searchForm.streetIds = [];
        this.searchForm.provinceId = '';
        this.searchForm.cityId = '';
        this.searchForm.areaId = '';
        this.searchForm.streetId = '';
      }
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    statusClass(val) {
      switch (val) {
        case 1:
          val = 'default';
          break;
        case 5:
          val = 'warning';
          break;
        case -1:
          val = 'danger';
          break;
        case 10:
          val = 'success';
          break;
      }
      return val;
    },
    operateClick(e, type) {
      // 1 查看 2 取消 3成功
      switch (type) {
        case 1:
          this.$router.push({
            path: '/message/alarmListDetail?alarmId=' + e.alarmId,
          });
          break;
        case 2:
          this.setState(e.alarmId, 16);
          break;
        case 3:
          this.setState(e.alarmId, 20);
          break;
        case 4:
          this.setPhoneState(e.alarmId);
      }
    },
    setState(id, state) {
      alarmApi
        .alarmOperator({
          id: id,
          state: state,
        })
        .then((res) => {
          if (res.status === 0) {
            this.$message.success('操作成功');
            this.listTable();
          }
        })
        .catch((error) => {
          this.$message.fail(error.msg);
        });
    },

    setPhoneState(id) {
      alarmApi
        .setCallState({
          id: id,
          state: 1,
        })
        .then((res) => {
          if (res.status === 0) {
            this.$message.success('操作成功');
            this.listTable();
          }
        })
        .catch((error) => {
          this.$message.fail(error.msg);
        });
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        alarmState: this.searchForm.alarmState,
        alarmType: this.searchForm.alarmType,
        villageId: this.searchForm.villageId,
        alarmNo: this.searchForm.alarmNo,
        provinceId: this.searchForm.provinceId,
        cityId: this.searchForm.cityId,
        areaId: this.searchForm.areaId,
        streetId: this.searchForm.streetId,
      };

      this.$emit('show-loading', true);
      alarmApi
        .alarmList(data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.total = res.data.total;
          this.$refs.tableList.bodyWrapper.scrollTop = 0;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          this.$emit('show-loading', false);
        });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    async getVillageList() {
      const { data } = await VillageApi.villageList();
      this.villageList_s = data || [];
    },
  },
  filters: {
    statusFilters(val) {
      switch (val) {
        case 1:
          val = '待出警';
          break;
        case 5:
          val = '已出警';
          break;
        case 15:
          val = '已取消';
          break;
        case 16:
          val = '取消失败';
          break;
        case 20:
          val = '已完成';
          break;
      }
      return val;
    },
    alarmSourceFilters(val) {
      switch (val) {
        case 1:
          val = '呼叫器';
          break;
        case 2:
          val = '小程序';
          break;
      }
      return val;
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
